<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="商品编码" name="goodCode">
						<a-input v-model:value="searchFormState.goodCode" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="主条码" name="barcode">
						<a-input v-model:value="searchFormState.barcode" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="商品名称" name="goodName">
						<a-input v-model:value="searchFormState.goodName" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => formRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="syncBatch()">
						同步至有赞
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'picture'">
					<a-avatar shape="square" :src="record.picture"/>
				</template>
				<template v-if="column.dataIndex === 'categoryInfo'">
					<div>
						<div>分类编码：{{record.categoryCode}}</div>
						<div>分类名称：{{record.categoryName}}</div>
					</div>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<div>
						<a-button type="link" size="small"  @click="sync(record)">同步</a-button>
					</div>
				</template>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="miniappGood">
	import { message } from 'ant-design-vue'
	import platformApi from '@/api/platform/platformApi'
	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	let adjustAccountForm = ref()
	let voucherForm = ref()
	const formData = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: true }
	const modalVisible = ref(false)
	const inputCode = ref(null)
	const confirmLoading = ref(false)
	const columns = [
		{
			title: '主图',
			dataIndex: 'picture',
			align: 'center'
		},
		{
			title: '商品编码',
			dataIndex: 'goodCode',
			width: 120
		},
		{
			title: '商品条码',
			dataIndex: 'barcode',
			width: 160
		},
		{
			title: '商品名称',
			dataIndex: 'goodName',
		},
		{
			title: '分类',
			dataIndex: 'categoryInfo',
			width: 300
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 200
		}
	]
	let selectedRowKeys = ref([])
	let selectedRows = ref([])
	let loading = ref(false)
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
				selectedRows = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRow) => {
				selectedRowKeys.value = selectedRowKey
				selectedRows.value = selectedRow
			}
		}
	}
	// 列表数据
	const loadData = (parameter) => {
		const params = Object.assign(parameter, searchFormState)
		return platformApi.goodPage(params).then((res) => {
			return res
		})
	}
	const sync = (record) => {
		const params = [record.goodCode]
		platformApi.batchCreateAppGood(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	// 批量同步
	const syncBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRows.value.map((m) => {
			return m.goodCode
		})
		platformApi.batchCreateAppGood(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style scoped>

</style>
